from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api import api_router
from app.config import settings
from app.database.base import Base
from app.database.session import engine
from app.core.logging import setup_logging
from app.core.middlewares import LoggingMiddleware
# 初始化日志
logger = setup_logging()
# 创建数据库表
Base.metadata.create_all(bind=engine)
# 创建FastAPI应用
app = FastAPI(
    title=settings.PROJECT_NAME,
    version=settings.PROJECT_VERSION,
    description="鹰迅AI聊天系统API"
)

# 添加日志中间件
app.add_middleware(LoggingMiddleware)

# 记录应用启动信息
logger.info(f"应用启动: {settings.PROJECT_NAME} v{settings.PROJECT_VERSION}")

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 生产环境应指定具体域名
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册API路由
app.include_router(api_router)

# 根路径
@app.get("/")
def read_root():
    logger.info("访问根路径")
    return {
        "project": settings.PROJECT_NAME,
        "version": settings.PROJECT_VERSION,
        "message": "欢迎使用鹰迅AI聊天系统"
    }



